package com.hl.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.Question;
import com.hl.sfzy.service.QuestionServue;
import com.hl.sfzy.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/question")
public class QuestionController {
	@Autowired
	private QuestionServue questionServue;
//	添加
	@PostMapping("/add")
	public R add(@RequestBody Question question) {
		int add = questionServue.add(question);
		if (add != 0) {
			return R.ok(add);
		} else {
			return R.fail("添加失败");
		}
	}
//	修改
	@PutMapping("/edit")
	public R edit(@RequestBody Question question) {
		int edit = questionServue.edit(question);
		if (edit != 0) {
			return R.ok(edit);
		} else {
			return R.fail("修改失败");
		}
	}
//  删除
	@DeleteMapping("/remove")
	public R remove(Integer questionId) {
		int remove = questionServue.remove(questionId);
		if (remove != 0) {
			return R.ok(remove);
		} else {
			return R.fail("删除失败");
		}
	}
//	批量删除
	@DeleteMapping("/removeBatch")
	public R removeBatch(Integer[] questionIds) {
		int removeBatch = questionServue.removeBatch(questionIds);
		if (removeBatch != 0) {
			return R.ok(removeBatch);
		} else {
			return R.fail("批量删除失败");
		}
	}
//	全部查询
	@GetMapping("/find")
	public R find() {
		List<Question> questions = questionServue.find();
		return R.ok(questions);
	}
//	分页查询
	@GetMapping("/findByPage")
	private R findByPage(
			@RequestParam(required = false ,defaultValue = "1")int pageNum,
			@RequestParam(required = false,defaultValue = "5") int pageSize, Question question) {
		PageInfo<Question> questions = questionServue.findByPage(pageNum, pageSize, question);
		return R.ok(questions);
	}
//	根据id查询
	@GetMapping("/findById")
	private R findById(Integer questionId) {
		Question question = questionServue.findById(questionId);
		return R.ok(question);
	}
}
